//SQLiteAdapter.java

package edu.cmu.cs214.hw9.db;

import java.sql.*;


public class SQLiteAdapter {
	protected Connection conn;
	
	//if we want multiple databases, this method can be changed to specify which connection to use
	//ex. public SQLiteAdapter(String connection)
	// ADDED TO THIS CODE FOR SHARDING -SARAH G. SPOCK  . ADDED TO CONNECT TO MULTIPLE DATABASES
	public SQLiteAdapter(String databaseName) throws Exception {
		Class.forName(Constants.JDBC_NAME);
		
		//connects to facelook.db
		conn = DriverManager.getConnection("jdbc:sqlite:" + databaseName);
	}
	
	public void createTables(){
		Statement stat;
		try {
			stat = conn.createStatement();
			
			//Check to see if users table already exists.  If so, drop it, because we
			//are resetting the database everytime the server restarts.
			
			//^^ We don't have to do that^^
//			DatabaseMetaData meta = conn.getMetaData();
//			ResultSet rs = meta.getTables(null, null, "users", null);
//			if (rs.next())
//			{
				stat.executeUpdate("DROP TABLE users");
//			}
			//name, email, password, friends, friend requests, notifications, statuses
			//first 3 are "short" strings of 50 characters or less.  last 8 represent string representations
			//of JSONArrays, from which we will parse into more useful lists
			stat.executeUpdate("CREATE TABLE users (username varchar(50), email varchar(50), password varchar(50)," +
					" friends text, subs text, pending text, notifs text, status text)");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			return;
		}
	}


}